package com.zzw.junit5demo;

import com.zzw.junit5demo.extension.IgnoreExceptionExtension;
import com.zzw.junit5demo.extension.Random;
import com.zzw.junit5demo.extension.RandomNumberExtension;
import com.zzw.junit5demo.extension.TimingExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;


/**
 * @author zzw
 * @version 1.0
 * @title RandomNumberDemo
 * @description <随机数扩展 及 异常忽略扩展>
 * @datetime 2023-06-14 22:48
 */
@ExtendWith({ RandomNumberExtension.class,
        MockitoExtension.class,
        IgnoreExceptionExtension.class})
public class RandomNumberDemo {

    private AutoCloseable autoCloseable;

    @Mock
    private List<String> list;

    @Random
    private int randomNumber1;

    @RegisterExtension
    static TimingExtension timingExtension = new TimingExtension();

    RandomNumberDemo(@Random int randomNumber2) {
        System.out.println("构造函数: " + randomNumber2);
    }


    @BeforeEach
    void beforeEach(@Random int randomNumber3) {
        // autoCloseable = MockitoAnnotations.openMocks(this);
        System.out.println("beforeEach: " + randomNumber3);
    }

    @Test
    void test(@Random int randomNumber4) {
        System.out.println("test: " + randomNumber1);
        System.out.println(randomNumber4);
    }

    @Test
    void testMock(){
        Mockito.when(list.get(0)).thenReturn("foo");
        Assertions.assertEquals("foo", list.get(0));
        list.add(String.valueOf(randomNumber1));
        System.out.println(list.get(0));
    }


    @Test
    void testException(){
        int total = 0;
        int  a = 1/total;
        System.out.println(a);
    }

    @AfterEach
    void afterEach() {
        autoCloseable.toString();
    }
}


